const log = console.log,
      users = require('../models/users'),
      {JWT_KEY} = require('../lib/constant') 
      router = require('koa-router')(),
      _ = require('lodash'),
      jwt = require('jsonwebtoken');

router.prefix('/api/users');
router.post('/login', async(ctx, next) => {
  let {email, password} = ctx.request.body;
  let res = await users.login(email, password);
  if(res.code === 0){
    //generate token
    //send token to client
    //res.data = token
    let token = await jwt.sign(_.toPlainObject(res.data), JWT_KEY);
    res.data = token;
  }
  ctx.body = res;
});

module.exports = router;
